﻿using System;
using LinQProject;
using ServiceProject.EntityService;

public partial class controls_ucLogin : System.Web.UI.UserControl
{
    public bool IsLogged
    {
        get
        {
            bool result;
            if (Boolean.TryParse(hdnLog.Value, out result))
                return result;
            return false;
        }
        set
        {
            hdnLog.Value = value.ToString();
        }
    }
    
    protected UserService userService=new UserService(database);
    private static readonly FarmacieDataClassesDataContext database = new FarmacieDataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
            DisplayLog(IsLogged);
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        IsLogged = userService.IsCorectUserAndPassword(txtUser.Text, txtPassword.Text);
        if (IsLogged)
        {
            Session["username"] = txtUser.Text;
            User user=userService.GetUserByUserName((string) Session["username"]);
            if (user !=null) Session["userId"] =user.UserId;
        }
        lblShowUser.Text = (string) Session["username"];
        DisplayLog(IsLogged);
            
    }

    private void DisplayLog(bool isLogged)
    {
        txtUser.Visible = !isLogged;
        txtPassword.Visible = !isLogged;
        lblUser.Visible = true;
        lblShowUser.Visible =isLogged;
        lblShowUser.Text = Session["username"].ToString();
        lblPassword.Visible = !isLogged;
        btnLogin.Visible = !isLogged;
        btnLogout.Visible = isLogged;
    }

    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Session["username"] = string.Empty;
        DisplayLog(false);
    }
}
